package com.itheima.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.cargo.*;
import com.itheima.domain.cargo.*;
import com.itheima.domain.vo.ExportProductResult;
import com.itheima.domain.vo.ExportResult;
import com.itheima.service.cargo.ExportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ExportProductDao exportProductDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ExtEproductDao extEproductDao;

    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    public void save(Export export) {
        int proNum = 0;
        int extNum = 0;
        List<ExportProduct> exportProducts = new ArrayList<>();

        export.setState(0);
        String[] contractIds = export.getContractIds().split(",");
        //遍历合同id
        for (String contractId : contractIds) {
            //处理合同主表
            Contract contract = contractDao.selectByPrimaryKey(contractId);
            contract.setState(2);
            contractDao.updateByPrimaryKeySelective(contract);

            //处理报运货物表
            ContractProductExample example = new ContractProductExample();
            ContractProductExample.Criteria criteria = example.createCriteria();
            criteria.andContractIdEqualTo(contractId);
            List<ContractProduct> contractProducts = contractProductDao.selectByExample(example);

            //遍历合同货物
            for (ContractProduct contractProduct : contractProducts) {
                ExportProduct exportProduct = new ExportProduct();
                BeanUtils.copyProperties(contractProduct, exportProduct);
                exportProduct.setId(UUID.randomUUID().toString());
                exportProduct.setExportId(export.getId());
                exportProductDao.insertSelective(exportProduct);
                proNum += exportProduct.getCnumber();

                exportProducts.add(exportProduct);

                //处理报运附件表
                ExtCproductExample extCproductExample = new ExtCproductExample();
                ExtCproductExample.Criteria extCCriteria = extCproductExample.createCriteria();
                extCCriteria.andContractProductIdEqualTo(contractProduct.getId());
                List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);

                //遍历合同附件
                for (ExtCproduct extCproduct : extCproducts) {
                    ExtEproduct extEproduct = new ExtEproduct();
                    BeanUtils.copyProperties(extCproduct, extEproduct);
                    extEproduct.setId(UUID.randomUUID().toString());
                    extEproduct.setExportId(export.getId());
                    extEproduct.setExportProductId(exportProduct.getId());
                    extEproductDao.insertSelective(extEproduct);

                    extNum += extEproduct.getCnumber();
                }
            }
        }

        //处理报运
        export.setProNum(proNum);
        export.setExtNum(extNum);
        export.setExportProducts(exportProducts);
        exportDao.insertSelective(export);
    }

    public void update(Export export) {
        exportDao.updateByPrimaryKeySelective(export);

        if (export.getExportProducts() != null) {
            for (ExportProduct exportProduct : export.getExportProducts()) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    public void delete(String id) {
        exportDao.deleteByPrimaryKey(id);

        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        exportProductDao.deleteByExample(exportProductExample);

        ExtEproductExample extEproductExample = new ExtEproductExample();
        extEproductExample.createCriteria().andExportIdEqualTo(id);
        extEproductDao.deleteByExample(extEproductExample);
    }

    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void updateE(ExportResult exportResult) {
        //1、根据exportResult更新export实体类
        Export export = new Export();
        export.setId(exportResult.getExportId());
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        //2、将export更新到数据库
        exportDao.updateByPrimaryKeySelective(export);
        //3、exportResult.products循环
        for (ExportProductResult exportProductResult : exportResult.getProducts()) {
            //4、更新exportProduct实体类
            ExportProduct exportProduct = new ExportProduct();
            exportProduct.setId(exportProductResult.getExportProductId());
            exportProduct.setTax(exportProductResult.getTax());
            //5、将exportProduct更新到数据库
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }
    }
}
